ass Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> ret;
        sort(nums.begin(), nums.end());
        for(int i = 0; i < nums.size();)
        {
            if(nums[i] > 0) break;
            int target = -nums[i];
            int left = i + 1, right = nums.size() - 1;
            while(left < right)
            {
                if(nums[left] + nums[right] > target)
                {
                    right--;
                }
                else if(nums[left] + nums[right] < target)
                {
                    left++;
                }
                else
                {
                    ret.push_back({nums[i], nums[left], nums[right]});
                    left++, right--;
                    while(left < right && nums[left - 1] == nums[left]) left++;
                    while(left < right && nums[right + 1] == nums[right]) right--;
                }
            }
            i++;
            while(i < nums.size() && nums[i - 1] == nums[i]) i++;
        }
        return ret;
    }
};
